<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;

class MusicController extends Controller
{
    public function index()
    {
        //查询数据
        $result = DB::table('music')->get();
        //跳转页面
        return view('admin/music', ['result' => $result]);
    }

    public function add()
    {
        //跳转页面
        return view('admin/musicadd');
    }

    //音乐添加方法实现
    public function insert(Request $request)
    {
        //获取信息
        $url = $request->url;
        $name = $request->name;
        $author = $request->author;
        $img = $request->img;
        //存入数组
        $data = array(
            'url' => $url,
            'name' => $name,
            'author' => $author,
            'img' => $img
        );
//        //存入数据库
        $result = DB::table('music')->insertGetId($data);
        //判断接收id
        if ($result) {
            //查询数据库
            $id = DB::table('music')->pluck('id')->toArray();
            if (count($id) == 1) {
                return redirect('/adm/music');
            }
            foreach ($id as $v) {
                if ($v !== $result) {
                    $del = DB::delete("delete from `music` where id=" . $v);
                }
            }
            if ($del) {
                //如果是return的话 返回到列表页面
                return redirect('/prompt')->with(['message'=>'添加音乐成功！','url' =>'/adm/music', 'jumpTime'=>3,'status'=>true]);
            }else{
                return redirect('/prompt')->with(['message'=>'添加音乐失败！','url' =>'/adm/music/add', 'jumpTime'=>3,'status'=>true]);
            }
        }
    }

    //删除ajax
    public function del($id)
    {
        //接收数据
        $id = $id;

        $result = DB::delete('delete from `music` where id=' . $id);

        if ($result) {
            echo 1;
        } else {
            echo 2;
        }
    }

    //后台编辑页面
    public function upd($id)
    {
        //接收id
        $id = $id;
        //查询数据
        $result = DB::table('music')->where('id', $id)->get();
        //跳转页面 传送数据
        return view('admin/musicupd', ['result' => $result]);
    }

    //后台音乐编辑方法
    public function update(Request $request)
    {
        //接收id
        $id = $request->id;
        //数据信息
        $url = $request->url;
        $name = $request->name;
        $author = $request->author;
        $img = $request->img;

        //放入数组里
        $data = array();
        //修改
        $result = DB::table('music')
            ->where('id', $id)
            ->update(
                [
                    'url' => $url,
                    'name' => $name,
                    'author' => $author,
                    'img' => $img
                ]
            );
        if ($result){
            //如果是return的话 返回到列表页面
            return redirect('/prompt')->with(['message'=>'编辑音乐成功！','url' =>'/adm/music', 'jumpTime'=>3,'status'=>true]);
        }else{
            return redirect('/prompt')->with(['message'=>'编辑音乐失败！','url' =>'/adm/music/upd/'.$id, 'jumpTime'=>3,'status'=>true]);
        }

    }


}
